package com.gxkj.qiao.alions.listener;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.gxkj.common.exceptions.ValidateException;
import com.gxkj.common.listeners.WebContextListener;
import com.gxkj.qiao.entitys.Movie;
import com.gxkj.qiao.services.MessageService;
import com.gxkj.qiao.services.MovieService;
import com.gxkj.qiao.services.impl.MessageServiceImpl;
import com.gxkj.qiao.services.impl.MovieServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * Created by dell on 2016/7/26.
 */
public class ConsumeMessageListener  implements MessageListener {
    Logger LOG = LoggerFactory.getLogger(ConsumeMessageListener.class);
    MessageService messageService = null;
    MovieService movieService = null;


    public Action consume(Message message, ConsumeContext context) {
        System.out.println("Receive: " + message.getMsgID());
        try {
            //do something..
            doSomeThing (message);
            return Action.CommitMessage;
        }catch (Exception e) {
            e.printStackTrace();
            //消费失败
            return Action.ReconsumeLater;
        }
    }

    public MessageService getMessageService(){

        if(messageService == null){
            synchronized (ConsumeMessageListener.class){
                if(messageService == null){
                    messageService = (MessageService) WebContextListener.getBean("messageServiceImpl");
                    if(messageService == null){
                        return getMessageService();
                    }
                }
            }
        }
        return messageService;
    }

    public MovieService getMovieService() {
        if(movieService == null){
            synchronized (ConsumeMessageListener.class){
                if(movieService == null){
                    movieService = (MovieService) WebContextListener.getBean("movieServiceImpl");
                    if(movieService == null){
                        return getMovieService();
                    }
                }
            }
        }
        return movieService;
    }

    private void doSomeThing(Message message) throws ValidateException {


        byte[] bytes = message.getBody();
        String msg_string = new String(bytes);
        LOG.info("Receive info :"+msg_string);
        JSONObject jsonObject_msg = JSONObject.parseObject(msg_string);
        String type = jsonObject_msg.getString("type");
        JSONObject entity = jsonObject_msg.getJSONObject("entity");

        if("message".equals(type)){
            messageService =  getMessageService();
            com.gxkj.qiao.entitys.Message insertEntity = JSONObject.toJavaObject(entity, com.gxkj.qiao.entitys.Message.class);

            Date now = new Date();
            insertEntity.setCreatedAt(now);
            insertEntity.setUpdatedAt(now);
            messageService.doAddMsg(insertEntity);
        }else   if("movie".equals(type)){
            movieService = getMovieService();


            Movie insertEntity = JSONObject.toJavaObject(entity, Movie.class);
            Movie dbMovie =  movieService.getMovieByDirectAndTile(insertEntity.getMovieDirect(),insertEntity.getMovieTitle());
            if(dbMovie != null){

            }else {
                Date now = new Date();
                insertEntity.setCreatedAt(now);
                insertEntity.setUpdatedAt(now);
                movieService.doAddMovie(insertEntity);
            }

        }


    }
}